package com.jiyun.controller;

import cn.hutool.json.JSONUtil;

import cn.hutool.jwt.JWTUtil;

import com.jiyun.entity.User;


import com.jiyun.entity.day06.Course;
import com.jiyun.entity.day06.Labels;
import com.jiyun.entity.day06.Menu;
import com.jiyun.feign.*;
import com.jiyun.vo.ResponseBean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserFeign userFeign;
    @Resource
    private CourseFeign courseFeign;
    @Resource
    private ContestFeign contestFeign;
    @Resource
    private TrainingFeign trainingFeign;
    @Resource
    private LabelsFeign labelsFeign;
    @Resource
    private MenuFeign menuFeign;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 登录校验
     * @param user
     * @return
     */
    @RequestMapping("/login")
    public ResponseBean login(@RequestBody User user){
        List<User> all = userFeign.findAll();
        System.out.println("all = " + all);
        User login = all.stream()
                .filter(users -> users.getUsername().equals(user.getUsername())
                        && users.getPassword().equals(user.getPassword()))
                .findFirst()
                .orElse(null);
        if(login==null){
            return ResponseBean.error("账号或密码错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId",login.getId());
        String token = JWTUtil.createToken(map, "123".getBytes());
        stringRedisTemplate.opsForValue().set("token",token);
        stringRedisTemplate.opsForValue().set("user", JSONUtil.toJsonStr(login));
        return ResponseBean.success("1",token);
    }

    /**
     * 查看所有课程
     * @return
     */
    @RequestMapping("/findByCourse")
    public ResponseBean findByCourse(){
        return courseFeign.findByName();
    }

    /**
     * 查看所有竞赛
     * @return
     */
    @RequestMapping("/findByContest")
    public ResponseBean findByContest(){
        return  contestFeign.findByName();
    }

    /**
     * 根据redis中用户查询所有竞赛
     * @return
     */
    @RequestMapping("/findByTraining")
    public ResponseBean findByTraining(){
        return trainingFeign.findByName();
    }

    /**
     * 根据课程id查询学习人数
     * @param id
     * @return
     */
    @RequestMapping("/findByCourseId")
    public ResponseBean findByCourseId(@RequestParam Integer id){
        return userFeign.findByCourseId(id);

    }

    /**
     * 根据id修改点赞数量
     * @param id
     * @param likes
     * @return
     */
    @RequestMapping("/update")
    public ResponseBean update(@RequestParam Integer id,@RequestParam Integer likes){
        return courseFeign.update(id,likes);
    }
    @RequestMapping("/findByUserMax")
    public ResponseBean findByUserMax(){
        return courseFeign.findByUserMax();
    }
    /**
     * 查询最新课程
     * @return
     */
    @RequestMapping("/findUserNewTime")
    public ResponseBean findUserNewTime(){
        return courseFeign.findUserNewTime();
    }
    /**
     * 查询最新培训
     * @return
     */
    @RequestMapping("/findTrainingNewTime")
    public ResponseBean findTrainingNewTime(){
        return trainingFeign.findTrainingNewTime();
    }
    /**
     * 查询最新竞赛
     * @return
     */
    @RequestMapping("/findContestNewTime")
    public ResponseBean findContestNewTime() {
        return contestFeign.findContestNewTime();
    }

    /**
     * 点赞功能
     * @param id
     * @return
     */
    @RequestMapping("/dianZanPlus")
    public ResponseBean dianZanPlus(@RequestParam Integer id){
        return courseFeign.dianZanPlus(id);
    }

    /**
     * 查询所有标签
     * @return
     */
    @RequestMapping("/findLabels")
    public List<Labels> findLabels(){
        return labelsFeign.findAll();
    }

    /**
     * 查询所有课程 带模糊查询
     * @param course
     * @return
     */
    @RequestMapping("/findCourseAll")
    public ResponseBean findCourseAll(@RequestBody Course course){
        return courseFeign.findByAll(course);
    }

    /**
     * 待条件查询菜单
     * @param menu
     * @return
     */
    @RequestMapping("/findMenuAll")
    public ResponseBean findMenuAll(@RequestBody Menu menu){
        return menuFeign.findPage(menu);
    }

    /**
     * 添加用户菜单中间表数据
     * @param id
     * @return
     */
    @RequestMapping("/userMenuAdd")
    public ResponseBean userMenuAdd(@RequestParam("id") Integer id){
        return menuFeign.userMenuAdd(id);
    }
    //查询所有战队
    @RequestMapping("/findSquadsAll")
    public ResponseBean findSquadsAll(){
        return userFeign.findSquadsAll();
    }
}
